package com.yanqu.road.dao.impl.relatives;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.relatives.RelativesInfo;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class RelativesDaoImpl extends TempDataObjectDao<RelativesInfo> {

    public List<RelativesInfo> getRelativesInfoList(long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        List<RelativesInfo> dataList = new ArrayList<>();
        String sql = "select * from t_u_relatives where userId1 = ? or userId2 = ?";
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(sql);
            call.setLong(1, userId);
            call.setLong(2, userId);
            rs = call.executeQuery();
            while (rs.next()) {
                RelativesInfo data = parse(rs);
                dataList.add(data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return dataList;
    }

    public Map<Long, RelativesInfo> getRelativesInfoMap(long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Long, RelativesInfo> dataMap = new ConcurrentHashMap<>();
        String sql = "select * from t_u_relatives where userId1 = ? or userId2 = ?";
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(sql);
            call.setLong(1, userId);
            call.setLong(2, userId);
            rs = call.executeQuery();
            while (rs.next()) {
                RelativesInfo data = parse(rs);
                if (data.getUserId1() == userId) {
                    dataMap.put(data.getUserId2(), data);
                } else {
                    dataMap.put(data.getUserId1(), data);
                }
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return dataMap;
    }

    @Override
    protected boolean addData(RelativesInfo data) {
        PreparedStatement call = null;
        String sql = "INSERT INTO t_u_relatives(userId1,userId2,marriageTimes,status,letterNum1,letterNum2,isShow) values (?,?,?,?,?,?,?) ";
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareStatement(sql);
            int i = 1;
            call.setLong(i++, data.getUserId1());
            call.setLong(i++, data.getUserId2());
            call.setInt(i++, data.getMarriageTimes());
            call.setShort(i++, data.getStatus());
            call.setInt(i++, data.getLetterNum1());
            call.setInt(i++, data.getLetterNum2());
            call.setString(i++, data.getIsShow());
            return call.executeUpdate() > 0;
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, null);
        }
        return false;
    }

    @Override
    protected boolean updateData(RelativesInfo data) {
        PreparedStatement call = null;
        String sql = "UPDATE t_u_relatives SET marriageTimes=?,status=?,letterNum1=?,letterNum2=?,isShow=? where userId1=? and userId2=?";
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareStatement(sql);
            int i = 1;
            call.setInt(i++, data.getMarriageTimes());
            call.setShort(i++, data.getStatus());
            call.setInt(i++, data.getLetterNum1());
            call.setInt(i++, data.getLetterNum2());
            call.setString(i++, data.getIsShow());
            call.setLong(i++, data.getUserId1());
            call.setLong(i++, data.getUserId2());
            return call.executeUpdate() > 0;
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, null);
        }
        return false;
    }

    @Override
    protected RelativesInfo parseData(ResultSet rs) throws SQLException {
        RelativesInfo data = new RelativesInfo();
        data.setUserId1(rs.getLong("userId1"));
        data.setUserId2(rs.getLong("userId2"));
        data.setMarriageTimes(rs.getInt("marriageTimes"));
        data.setStatus(rs.getShort("status"));
        data.setLetterNum1(rs.getShort("letterNum1"));
        data.setLetterNum2(rs.getShort("letterNum2"));
        data.setIsShow(rs.getString("isShow"));
        return data;
    }
}
